<?php

namespace Bank\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class BankController extends AbstractActionController
{
    protected $bank;
    protected $bank_id;
    protected $bankTable;
    protected $staffTable;
    protected $accountTable;
    protected $loanTable;
    protected $check;
    protected $thrift;
    protected $account;
    protected $client_id;
    protected $payment;
    protected $check_profit;
    protected $thrift_profit;
    protected $account_profit;
    protected $check_balance;
    protected $thrift_balance;
    protected $account_balance;

    public function __construct($bank_id = null)
    {
        $this->bank_id = $bank_id;
    }

    public function getStaffTable()
    {
        if (!$this->staffTable) {
            $sm = $this->getServiceLocator();
            $this->staffTable = $sm->get('Staff\Model\StaffTable');
        }
        return $this->staffTable;
    }

    public function getAccountTable()
    {
        if (!$this->accountTable) {
            $sm = $this->getServiceLocator();
            $this->accountTable = $sm->get('Account\Model\AccountTable');
        }
        return $this->accountTable;
    }

    public function getLoanTable()
    {
        if (!$this->loanTable) {
            $sm = $this->getServiceLocator();
            $this->loanTable = $sm->get('Loan\Model\LoanTable');
        }
        return $this->loanTable;
    }

    public function getBankTable()
    {
        if (!$this->bankTable) {
            $sm = $this->getServiceLocator();
            $this->bankTable = $sm->get('Bank\Model\BankTable');
        }
        return $this->bankTable;
    }

    /**
     * 计算账户总利润
     */
    public function setAccountProfit()
    {
        $this->payment = 0;
        $this->check_profit = 0;
        $this->thrift_profit = 0;
        $this->account_profit = 0;
        $this->check_balance = 0;
        $this->thrift_balance = 0;
        $this->account_balance = 0;
        $this->check = $this->getAccountTable()->getCheckByBankId($this->bank_id);
        $this->thrift = $this->getAccountTable()->getThriftByBankId($this->bank_id);
        $this->account = $this->getAccountTable()->getAccountByBankId($this->bank_id);
        foreach ($this->check as $key => $check) {
            $this->payment += $check['account_balance'];
            $this->check_profit += $check['account_balance'] * $check['rate'];
            $this->check_balance += $check['overdraft'] - $check['account_balance'];
        }
        foreach ($this->thrift as $key => $thrift) {
            $this->thrift_profit += $thrift['account_balance'] * $thrift['rate'];
            $this->thrift_balance += $thrift['account_balance'];
        }
        $this->account_profit = $this->check_profit + $this->thrift_profit;
        $this->account_balance = $this->check_balance + $this->thrift_balance;
    }

    public function indexAction()
    {
        $this->bank = $this->getBankTable()->fetchAll();
        $view = new ViewModel(array(
            'bank' => $this->bank
        ));
        return $view;
    }

    public function staffAction()
    {
        $this->bank_id = $this->params()->fromRoute('id', null);
        $staffs = $this->getStaffTable()->getStaffByBankId($this->bank_id);
        $view = new ViewModel(array(
            'staffs' => $staffs
        ));
        return $view;
    }

    public function accountAction()
    {
        $this->bank_id = $this->params()->fromRoute('id', null);
        $this->setAccountProfit();
        $view = new ViewModel(array(
            'check' => $this->check,
            'thrift' => $this->thrift,
            'account' => $this->account,
            'payment' => $this->payment,
            'check_profit' => $this->check_profit,
            'thrift_profit' => $this->thrift_profit,
            'account_profit' => $this->account_profit,
            'check_balance' => $this->check_balance,
            'thrift_balance' => $this->thrift_balance,
            'account_balance' => $this->account_balance
        ));
        return $view;
    }

    public function loanAction()
    {
        $this->bank_id = $this->params()->fromRoute('id', null);
        $loan = $this->getLoanTable()->getLoanByBankId($this->bank_id);
        $view = new ViewModel(array(
            'loan' => $loan
        ));
        return $view;
    }
}